/**Author:7rATM Team
 * Date:May 2009
 * Description: The class ClientDB models a database of 
 * clients. It uses a collection of clents instances
 *
 */
 

package AtmSystem;

import java.util.*;

public class ClientDB
{
        /* Collection of <code>clients</code> objects.*/
	private Vector<Client> clients;

      /**
       * Constructs an empty backacccounts database.
       */
	public ClientDB ()
		{
		clients=new Vector();
		}

    /**
     * Adds a {@link Client} object to this clients database.
     *
     * @param newClient  the {@link Client} object.
     */
	public boolean add(Client newClient)
		{
			return clients.add(newClient);
		}

    /**
     * Removes a {@link ClientDB} object from this collection
     * and sets the {@link ClientDB} object newUsername as available.
     *
     * @param number  the {@link ClientDB} object.
     * @return <code>ture</code> if user name is exit ;
     * return  <code>false</code>otherwises.
     */
	public boolean remove(String newUsername)
		{
		Client removedClient = isExsit(newUsername);
		if(removedClient != null)
			{
			clients.remove(removedClient);
			return true;
		}
		
		else return false;
		}

          /**
	   *@return the index of clients if newUsername equals username and
	   *     newPassword equals to password
	   *   return null otherwise
	   */
	public Client isExsit(String newUsername,String newPassword)
		{  
		for (int index=0;index<clients.size();index++ )
			{
				if  (clients.get(index).getUsername().equals(newUsername)
				&&clients.get(index).getPassword().equals(newPassword))
				{
				return (Client) ( clients.get(index) );
				}	
			}
		return null;
		}
          /**
	   * @return the index of clients if newUsername equals username
	   * return null otherwise
	   */
		public Client isExsit(String newUsername)
		{
			for(int index = 0;index<getNumbers();index ++)
			{
				if (clients.get(index).getUsername().equals(newUsername))
				{
					return (Client) ( clients.get(index) );
				}
			}
			return null;
		}

          /**
	   * @return the index of card number if newUsername equals username
	   * return null otherwise
	   */		
		public Client isExsit(int CardNumber)
		{
			for(int index = 0;index<getNumbers();index ++)
			{
				for(int i =0;i<get(index).getBankAccountDB().getNumbers();i++)
				{
					if (get(index).getBankAccountDB().get(i).getCardNumber()==CardNumber)
				return (Client) (get(index) );
				}
			}
			return null;
		}
    /**
     * Returns the number of clients 
     *
     * @return  the number of clients .
     */
	public int getNumbers()
		{
		return clients.size();
		}
    /**
     * Returns the index of clients
     *
     * @return  the index  of clients .
     */
		public Client get(int index)
		{
			return (Client) clients.get(index);
		}
}
